package com.fernfx.xingtan.main.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.common.base.BaseFragment;

import java.io.Serializable;

import butterknife.BindView;
import butterknife.OnCheckedChanged;

import static com.fernfx.xingtan.main.ui.RobredPacketHistoryFragment.REQUEST_URL;

/**
 * Created by admin on 2018/8/10.
 */

public class RobredPacketHistroyActivity extends BaseActivity {

    @BindView(R.id.finish_tv)
    TextView finishTv;
    //发送包fragment
    private final RobredPacketHistoryFragment sendRobredPacketHistoryFragment = new RobredPacketHistoryFragment();
    //收红包fragment
    private final RobredPacketHistoryFragment getRobredPacketHistroyFragment = new RobredPacketHistoryFragment();
    //当前fragment
    private BaseFragment currentFragment;
    private Bundle getHistroyBundle = new Bundle(),sendHistroyBundle=new Bundle();


    public static void start(Context context) {
        Intent intent = new Intent(context, RobredPacketHistroyActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_robred_packet_histroy;
    }

    @Override
    protected void initView() {
        //获取红包
        FragmentArgsEntity getEntity = new FragmentArgsEntity();
        getEntity.status = 1;
        getEntity.orderBy = "update_time";
        getEntity.url = "redPacket/checkRobRedPacket";
        getHistroyBundle.putSerializable(RobredPacketHistoryFragment.REQUEST_URL,getEntity);

        //发送红包
        FragmentArgsEntity sendEntity = new FragmentArgsEntity();
        sendEntity.status = 2;
        sendEntity.orderBy = "create_time";
        sendEntity.url = "redPacket/checkSendRedPacket";
        sendHistroyBundle.putSerializable(RobredPacketHistoryFragment.REQUEST_URL,sendEntity);
        //设置Bundle
        sendRobredPacketHistoryFragment.setArguments(sendHistroyBundle);
        getRobredPacketHistroyFragment.setArguments(getHistroyBundle);
        //默认启动的页面
        changeFragment(sendRobredPacketHistoryFragment);
    }

    @Override
    protected void initListener() {
        finishTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @OnCheckedChanged({R.id.send_robred_history_bt, R.id.get_robred_history_bt})
    public void onRadioButtonCheckChanged(CompoundButton button, boolean checked) {
        if (!checked) {
            return;
        }

        switch (button.getId()) {
            case R.id.get_robred_history_bt:
                //抢红包url
                changeFragment(getRobredPacketHistroyFragment);
                break;
            case R.id.send_robred_history_bt:
                //发红包url
                changeFragment(sendRobredPacketHistoryFragment);
                break;
        }
    }

    private void changeFragment(BaseFragment baseFragment){
        FragmentManager mFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
        if (currentFragment == null) {
            fragmentTransaction.add(R.id.histroy_lat, baseFragment).commit();
            currentFragment = baseFragment;
        }

        if (currentFragment != baseFragment) {
            // 先判断是否被add过
            if (!baseFragment.isAdded()) {
                // 隐藏当前的fragment，add下一个到Activity中
                fragmentTransaction.hide(currentFragment)
                        .add(R.id.histroy_lat, baseFragment).commit();
            } else {
                // 隐藏当前的fragment，显示下一个
                fragmentTransaction.hide(currentFragment).show(baseFragment)
                        .commit();
            }
            currentFragment = baseFragment;
        }
    }

    /*
     *参数类
     */
    public class FragmentArgsEntity implements Serializable{
        //1抢红包   2发红包
        public int status;

        public String url;

        public String orderBy;
    }
}
